package com.wbqianrushi;

/**
 * @author 黄衡熙
 * @version 1.0
 */

import java.util.*;

public class Test02_A2B游戏 {
    public static void main(String[] args) {
        test();
    }

    public static void test() {
        System.out.println(" ==================================================\n" +
                " 1A2B 猜数字游戏\n" +
                " ==================================================\n" +
                "\n" +
                " 欢迎来到 1A2B 猜数字游戏！\n" +
                " 我已经想好了一个 4 位数字（每位不重复且首位不为零）\n" +
                " 请试着猜出这个数字。\n" +
                " 我会用 A 表示数字正确且位置正确，B 表示数字正确但位置错误\n" +
                " 例如: 如果答案是 1234，你猜 1324，结果是 2A2B\n" +
                " --------------------------------------------------");
        //随机生成四个数字
        int[] arr = new int[4];
        int random1 = (int) (Math.random() * 8 + 1);
        arr[0] = random1;
        int count = 1;
        while (count <= 3) {
            int random2 = (int) (Math.random() * 9);
            boolean isDuplicate = false;
            for (int j = 0; j < count; j++) {
                if (arr[j] == random2) {
                    isDuplicate = true;
                }
            }
            if (!isDuplicate) {
                arr[count] = random2;
                count++;
            }
        }
        System.out.println(Arrays.toString(arr));


        Scanner sc = new Scanner(System.in);
        int guessCount = 0;//猜测次数
        List<String> history = new ArrayList<>();//存放猜测历史数据

        while (true) {
            System.out.print("请输入你的猜测（4位不重复数字）：");
            String input = sc.nextLine();
            // 验证输入长度
            if (input.length() != arr.length) {
                System.out.println("输入错误，请输入4个数字！");
                continue;
            }

            // 验证是否全为数字
            boolean isAllDigit = true;
            for (char c : input.toCharArray()) {
                if (!Character.isDigit(c)) {
                    isAllDigit = false;
                    break;
                }
            }
            if (!isAllDigit) {
                System.out.println("输入错误，请仅输入数字！");
                continue;
            }

            // 验证数字是否重复
            char[] chars = input.toCharArray();
            boolean isDuplicate = false;
            for (int i = 0; i < 4; i++) {
                for (int j = i + 1; j < 4; j++) {
                    if (chars[i] == chars[j]) {
                        isDuplicate = true;
                        break;
                    }
                }
                if (isDuplicate) break;
            }
            if (isDuplicate) {
                System.out.println("输入数字重复，请重新输入");
                continue;
            }

            // 将输入转换为数字数组
            int[] guess = new int[arr.length];
            for (int i = 0; i < 4; i++) {
                guess[i] = chars[i] - '0';
            }

            //统计
            int sumA = 0;
            for (int i = 0; i < guess.length; i++) {
                if (guess[i] == arr[i]) {
                    sumA++;
                }
            }

            int sumB = 0;
            for (int i = 0; i < guess.length; i++) {
                if (guess[i] == arr[i]) {
                    continue; // 已经算为A的不再算为B
                }
                for (int j = 0; j < guess.length; j++) {
                    if (i != j && guess[i] == arr[j]) {
                        sumB++;
                        break;
                    }
                }
            }


            // 更新猜测次数和历史记录
            guessCount++;
            String result = sumA + "A" + sumB + "B";
            System.out.println("第" + guessCount + "次猜测: " + input + " => " + result);

            // 记录历史
            history.add("#" + guessCount + ": " + input + " => " + result);

            // 显示历史记录
            System.out.println("历史记录:");
            for (String record : history) {
                System.out.println(record);
            }
            System.out.println();

            // 判断是否猜对
            if (sumA == 4) {
                System.out.println("恭喜！你猜对了！");
                System.out.println("正确答案是: " + input);
                System.out.println("你总共尝试了 " + guessCount + " 次");

                // 询问是否再玩一局
                System.out.print("是否再玩一局？(y/n): ");
                String playAgain = sc.nextLine().trim().toLowerCase();
                if (playAgain.equals("y")) {
                    System.out.println("--------------------------------------------------");
                    test();      // 重新开始游戏
                    return;      // 退出当前方法
                } else {
                    System.out.println("感谢游玩，再见！");
                    return;
                }
            }
        }
    }

}
